package pikes.core;

import java.io.IOException;

import pikes.util.Util;

/**
 * A bridge between the representation of a character sequence and a printable.
 * @author Peter Bona
 */
public class PrintableCharSequence extends AbstractPrintable {

	private CharSequence charSequence = null;

	/**
	 * Only constructor which takes a CharSequence argument
	 * @param charSequence The content
	 */
	public PrintableCharSequence(final CharSequence charSequence) {
		super();
		this.charSequence = charSequence;
	}

	/**
	 * Prints the content character sequence to appendable using append method.
	 * @param appendable The target
	 */
	public final void print(final Appendable appendable) throws IOException {
		if (charSequence != null) {
			appendable.append(charSequence);
		}
	}

	@Override
	public final boolean equals(final Object obj) {
		if (obj instanceof Printable) {
			Printable thatPrintable = (Printable) obj;
			return Util.equals(this, thatPrintable);
		}
		else if (obj instanceof CharSequence) {
			CharSequence thatCharSequence = (CharSequence) obj;
			return charSequence.equals(thatCharSequence);
		}

		return false;

	}

	@Override
	public final int hashCode() {
		return charSequence.hashCode();
	}

}
